using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BusinessLayer;
using EntityLayer;
using WebControls;

public partial class Admin_Files : SFPage
{
    busFiles insBusFiles = new busFiles();
    entFiles insEntFiles = new entFiles();
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected bool sfgFiles_NeedsDataSource()
    {
        DataTable insDataTableFiles = insBusFiles.SelectFiles();
        if (insBusFiles.HasErrors)
        {
            MessageBoxShow(insBusFiles);
        }
        sfgFiles.SetData(insDataTableFiles);
        return insBusFiles.HasErrors;
    }
    protected void btnNew_Click(object sender, EventArgs e)
    {
        this.Id = 0;
        this.OpenEditPanel();

    }
    protected void sfgFiles_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "EditData")
        {
            this.Id = Convert.ToInt32(e.CommandArgument);
            this.OpenEditPanel();
        }
        else if (e.CommandName == "DeleteData")
        {
            insEntFiles.FileId = Convert.ToInt32(e.CommandArgument);
            insBusFiles.DeleteFilesByFileId(insEntFiles);
            if (insBusFiles.HasErrors)
            {
                MessageBoxShow(insBusFiles);
                return;
            }
            sfgFiles.LoadGridData();
            this.CloseEditPanel();
        }
    }
    public override void OpenEditPanel()
    {
        this.Clear(this.Master.FindControl("ContentPlaceHolderEdit"));
        base.OpenEditPanel();
        lstFiles.LoadDataListData();

    }
    protected bool lstFiles_NeedsDataSource()
    {
        insEntFiles.FileId = this.Id;
        insBusFiles.SelectFilesByFileId(insEntFiles);
        if (insBusFiles.HasErrors)
        {
            MessageBoxShow(insBusFiles);
            return true;
        }
        txtFileDescription.Text = insEntFiles.FileDescription;
        calInsertDate.Value = insEntFiles.InsertDate;

        entFilesContents insEntFilesContents = new entFilesContents();
        insEntFilesContents.FileId = insEntFiles.FileId;
        busFilesContents insBusFilesContents = new busFilesContents();
        DataTable insDataTableFilesContents = insBusFilesContents.SelectFilesContentsByFileId(insEntFilesContents);
        if (insBusFilesContents.HasErrors)
        {
            MessageBoxShow(insBusFilesContents);
            return true;
        }
        lstFiles.SetData(insDataTableFilesContents);
        return false;
    }

    public override void Save()
    {

        this.Validate();
        if (this.HasErrors)
        {
            MessageBoxShow(this);
            return;
        }

        insEntFiles.FileDescription = txtFileDescription.Text;
        insEntFiles.InsertDate = calInsertDate.Value.Value;
        foreach (DataListItem dli in lstFiles.Items)
        {
            entFilesContents insEntFilesContents = new entFilesContents();
            if (((CheckBox)dli.FindControl("chkLanguage")).Checked)
            {
                insEntFilesContents.LanguageId = Convert.ToInt32(((SFCheckBox)dli.FindControl("chkLanguage")).Tag);
                insEntFilesContents.FileName = ((SFTextBox)dli.FindControl("txtFileName")).Text;
                insEntFilesContents.FilePath = ((SFTextBox)dli.FindControl("txtFilePath")).Text;
                if (!WebControls.Validation.ValidateWebSite(((SFTextBox)dli.FindControl("txtFilePath")).Text))
                {
                    MessageBoxShow(StaticMethods.GetErrorMessageMultilanguageString(18, new object[] { ((SFLabel)dli.FindControl("lblFilePath")).Text }));
                    return;
                }
                if (lstFiles.DataKeys[dli.ItemIndex] != DBNull.Value)
                {
                    insEntFilesContents.FilesContentId = Convert.ToInt32(lstFiles.DataKeys[dli.ItemIndex]);
                    insEntFiles.FilesContentsUpdate.Add(insEntFilesContents);
                }
                else
                {
                    insEntFiles.FilesContentsInsert.Add(insEntFilesContents);
                }
            }
            else
            {
                if (lstFiles.DataKeys[dli.ItemIndex] != DBNull.Value)
                {
                    insEntFilesContents.FilesContentId = Convert.ToInt32(lstFiles.DataKeys[dli.ItemIndex]);
                    insEntFiles.FilesContentsDelete.Add(insEntFilesContents);
                }
            }
        }
        base.Save();
    }
    public override void SaveNew()
    {
        insBusFiles.InsertFiles(insEntFiles);
        if (insBusFiles.HasErrors)
        {
            MessageBoxShow(insBusFiles);
            return;
        }
        base.SaveNew();
        sfgFiles.LoadGridData();
    }
    public override void SaveExisting()
    {
        insEntFiles.FileId = this.Id;
        insBusFiles.UpdateFilesByFileId(insEntFiles);
        if (insBusFiles.HasErrors)
        {
            MessageBoxShow(insBusFiles);
            return;
        }
        base.SaveExisting();
        sfgFiles.LoadGridData();
    }

    protected void lstFiles_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if (e.Item != null && e.Item.FindControl("chkLanguage") != null && e.Item.DataItem != null)
        {
            if (this.Id != 0)
            {
                ((SFCheckBox)e.Item.FindControl("chkLanguage")).Checked = (((DataRowView)e.Item.DataItem)["FilesContentId"] != DBNull.Value);
                e.Item.FindControl("pnlFilesContents").Visible = ((SFCheckBox)e.Item.FindControl("chkLanguage")).Checked;
            }
            else
            {
                ((SFCheckBox)e.Item.FindControl("chkLanguage")).Checked = true;
            }
            ((SFCheckBox)e.Item.FindControl("chkLanguage")).Tag = ((DataRowView)e.Item.DataItem)["LanguageId"].ToString();
            ((ImageButton)e.Item.FindControl("ibtUploadFile")).Attributes.Add("onclick", "OpenFileUpload('" + e.Item.FindControl("txtFilePath").ClientID + "','" + e.Item.FindControl("txtFileName").ClientID + "');return false;");
        }
    }
    protected void Check(object sender, System.EventArgs e)
    {
        this.OpenEditPanelBase();
        ((SFCheckBox)sender).NamingContainer.FindControl("pnlFilesContents").Visible = ((SFCheckBox)sender).Checked;
    }
}

